package flirtme.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.ans.flirtme.R;

import flirtme.libraries.imageloader.ImageLoader;
import flirtme.model.BroadcastMessage;
import flirtme.model.Message;

public class BroadcastMessageListAdapter extends BaseAdapter {
	
	private List<BroadcastMessage> messages;
	private LayoutInflater layoutInflater = null;
	private ImageLoader imageLoader = null;
	
	public BroadcastMessageListAdapter(Context context, List<BroadcastMessage> listData) {
		this.messages = listData;
		layoutInflater = LayoutInflater.from(context);
		imageLoader = new ImageLoader(context.getApplicationContext());
	}

	@Override
	public int getCount() {
		if (this.messages != null) {
			return messages.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int index) {
		return messages.get(index);
	}

	@Override
	public long getItemId(int index) {
		return messages.get(index).getId();
	}

	@Override
	public View getView(int index, View convertView, ViewGroup parent) {		
		ViewHolder holder;
		BroadcastMessage message = messages.get(index);
		
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.list_row_broadcast_msg, null);
			holder = new ViewHolder();
			holder.sender = (TextView) convertView.findViewById(R.id.broadcast_list_msg_sender);
			holder.preview = (TextView) convertView.findViewById(R.id.broadcast_list_msg_preview);
			holder.thumbImage =(ImageView) convertView.findViewById(R.id.broadcast_list_sender_thumbnail);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		ImageButton mapButton = (ImageButton) convertView.findViewById(R.id.broadcast_list_map_image);
		mapButton.setFocusable(false);
		mapButton.setTag(message.getId());
		
		holder.sender.setText(message.getSender().getName());
		holder.preview.setText(message.getMessagePreview());
		imageLoader.DisplayImage(message.getSender().getImageUrl(), holder.thumbImage);
		
		return convertView;
	}
	
	static class ViewHolder {
		TextView sender;
		TextView preview;
		ImageView thumbImage;
	}

}
